class_name FloatText
extends RichTextLabel

## TODO 加入弹出动画


const _CRITICAL_ICON_PATH := "res://assets/critical_icon.png"
const _NORMAL_FORMAT := "{text}"
const _CRITICAL_FORMAT := "[img=10x10]{icon}[/img]" + _NORMAL_FORMAT


@export var duration := 0.25
@export var move_duration := 0.15
@export var move_distance := 8
@export var font_size_range: Vector2i = Vector2i(10, 12)


func show_text(text_: String, color: Color, pos: Vector2) -> void:
    modulate = color

    var font_size: int = randi_range(font_size_range.x, font_size_range.y)
    set("theme_override_font_sizes/normal_font_size", font_size)

    text = text_

    # global_position = pos + Vector2(randi_range(-16, 16), randi_range(-8, 0))
    # global_position.x -= get_rect().size.x / 2

    global_position = pos
    global_position.x -= get_rect().size.x / 2

    await get_tree().create_timer(duration).timeout
    
    var hide_tween := get_tree().create_tween()
    hide_tween.tween_property(self, "modulate:a", 0, move_duration)
    hide_tween.finished.connect(ObjectPool.release_obj.bind(self))

    var move_tween := get_tree().create_tween()
    move_tween.tween_property(self, "global_position", global_position + Vector2(0, -move_distance), move_duration)
    
    hide_tween.play()
    move_tween.play()


func show_damage(info: DamageInfo) -> void:
    var color = info.get_damage_attribute_color()
    var data := {
        "text": "%.1f" % info.calced_damage,
        # "text": info.calced_damage,
        # "icon": "",
    }
    var format := _NORMAL_FORMAT

    if info.is_miss:
        color = Color.GRAY
        data["text"] = "miss"
    elif info.is_critical:
        format = _CRITICAL_FORMAT
        data["icon"] = _CRITICAL_ICON_PATH
    
    show_text(format.format(data), color, info.target.float_text_posistion())
